package cn.doitedu;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.roaringbitmap.longlong.Roaring64Bitmap;

import java.io.IOException;
import java.util.Date;

/**
 * @Author: 深似海
 * @Site: <a href="www.51doit.com">多易教育</a>
 * @QQ: 657270652
 * @Date: 2024/4/26
 * @Desc: 学大数据，上多易教育
 *   根据给定的活跃日期，生成bitmap字段
 **/
public class GenBitmapByDateArray extends UDF {
    private static final Date INITIAL = DateUtil.parse("2000-01-01");


    // select gen_days_bm(array('2024-04-01','2024-04-02','2024-04-05')) as bm
    public byte[] evaluate(String[] activeDates) throws IOException {

        Roaring64Bitmap bitmap = Roaring64Bitmap.bitmapOf();

        for (String activeDate : activeDates) {
            long between = DateUtil.between(INITIAL, DateUtil.parse(activeDate), DateUnit.DAY);
            bitmap.add(between);
        }

        return BitmapUtils.ser(bitmap);
    }


}
